<?php

namespace app\api\server\user;

use app\api\model\user\UserEntityCartApiModel;
use app\api\ServerBase;
use app\api\validate\order\OrderEntityVal;
use app\api\validate\user\UserCartVal;
use app\logic\single\EntitySingle;
use think\facade\Db;
use think\Request;

class UserEntityCartServer extends ServerBase
{

    protected $model;
    protected $request;
    /**
     * @var mixed
     */
    protected $param;
    /**
     * @var UserCartVal
     */
    private $validate;
    /**
     * @var array|int[]
     */
    private $type_arr;

    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new UserEntityCartApiModel();
        $this->validate = new OrderEntityVal();
        $this->type_arr = [10,20,30,40];
    }

    function getList(){
        $list = $this->model->where('user_id',$this->request->user_id)
            ->order('id desc')
            ->field('id,goods_id,type,is_select')
            ->select()->toArray();
        if(empty($list)){
            $this->getMessageCode('code_common.not_find',1);
            return false;
        }
        //判断对应的商品是否存在
        $list = EntitySingle::getInstance()->getGoodsList($list);




        $this->getMessageCode('code_common.success',2);
        return $list;
    }
    function add(){
        if(!$this->validate->scene('create')->check($this->param)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        $goods_id = $this->param['goods_id'];
        $type = $this->param['type'];
        if(!in_array($type,$this->type_arr)){
            $this->ERROR = '非法访问';
            return false;
        }
        //检测是否有重复的
        $exist = $this->model->checkExist($goods_id,$type,$this->request->user_id);
        if(!empty($exist)){
            $this->ERROR = '已在购物车';
            return false;
        }
        //判断对应的商品是否存在
        $goods = EntitySingle::getInstance()->getGoodsByType($goods_id,$type);
        if(empty($goods)){
            $this->ERROR = '商品不存在';
            return false;
        }
        Db::startTrans();
        try {
            $this->model->save([
                'user_id'=>$this->request->user_id,
                'goods_id'=>$goods_id,
                'type'=>$type,
            ]);
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR = $exception->getMessage();
            return false;
        }
        $this->getMessageCode('code_common.success',2);
        return true;
    }
    function setSelect(){
        $id = $this->param['id'];
        if(empty($id)){
            $this->ERROR = '非法访问';
            return false;
        }
        //检测是否存在
        $exist = $this->model->checkExistById($id,$this->request->user_id);
        if(empty($exist)){
            $this->ERROR = '不在购物车';
            return false;
        }
        //判断对应的商品是否存在
        $goods = EntitySingle::getInstance()->getGoodsByType($exist['goods_id'],$exist['type']);
        if(empty($goods)){
            $this->ERROR = '商品不存在';
            return false;
        }
        Db::startTrans();
        try {
            $is_select = 1;
            if($exist['is_select'] == 1){
                $is_select = -1;
            }
            $exist->is_select = $is_select;
            $exist->save();
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR = $exception->getMessage();
            return false;
        }
        $this->getMessageCode('code_common.success',2);
        return true;
    }

    function del(){
        $id = $this->param['id'];
        if(empty($id)){
            $this->ERROR = '非法访问';
            return false;
        }
        //检测是否存在
        $exist = $this->model->checkExistById($id,$this->request->user_id);
        if(empty($exist)){
            $this->ERROR = '不在购物车';
            return false;
        }
        Db::startTrans();
        try {
            $exist->delete();
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR = $exception->getMessage();
            return false;
        }
        $this->getMessageCode('code_common.success',2);
        return true;
    }
}